<?php

namespace app\api\controller\v35;

use app\api\controller\v2\Auth;
use app\common\cachemodel\zl2\Zl2FortuneDayTarotText;
use app\common\cachemodel\zl2\Zl2FortuneTarotCard;
use think\response\Json;

class FortuneTarotCard extends Auth
{
    /**
     * 转运卡牌记录列表
     * @return Json
     * @throws \Exception
     */
    public function getCardList()
    {
        $fortuneTarotCardModel = (new Zl2FortuneTarotCard());

        $pageData = $fortuneTarotCardModel->getListPage($this->user['id'], 'id', ['id', 'card_id', 'create_time']);

        $tarotConfig = config('tarot');
        $resData = [];
        foreach ($pageData['data'] as $dataItem)
        {
            $resData[] = [
                'id' => (int)$dataItem['id'],
                'card_img' => "/test/tarot/cards/{$dataItem['card_id']}-1.png",
                'name' => $tarotConfig['card_list'][$dataItem['card_id']]['name'],
                'time' => date('Y-m-d H:i', $dataItem['create_time']),
            ];
        }

        $pageData['data'] = $resData;

        return show(config('code.success'), '请求成功', $pageData);
    }

    /**
     * 转运卡牌 抽牌
     */
    public function getCard()
    {
        $date = (int)date('Ymd');
        $fortuneTarotCardModel = (new Zl2FortuneTarotCard());
        // 判断今日是否已经抽牌
        $id = $fortuneTarotCardModel->getCardIdByToday($this->user['id'], $date);

        if ($id)
        {
            $cardInfo = $fortuneTarotCardModel->getInfoById($id);

            $cardNum = $cardInfo['card_id'];

        } else
        {
            $cardNum = mt_rand(1, 78);

            $d = [
                'uid' => $this->user['id'],
                'card_id' => $cardNum,
                'direction' => 1,   // 转运卡牌只有正位
                'date' => $date,
                'create_time' => $this->request->time()
            ];

            $id = $fortuneTarotCardModel->saveData($this->user['id'], $d);
        }

        return show(config('code.success'), '请求成功', [
            'id' => $id,
            'card_img' => "/test/tarot/cards/{$cardNum}-1.png",
        ]);
    }

    /**
     * 获取抽牌结果
     * @return Json
     * @throws \Exception
     */
    public function details()
    {
        $id = input('id/d', 0);
        if (!$id)
        {
            return show(config('code.error'), '请求异常，请稍后再试～');
        }
        $fortuneTarotCardModel = new Zl2FortuneTarotCard();
        $cardInfo = $fortuneTarotCardModel->getInfoById($id);

        if (!$cardInfo || $cardInfo['uid'] !== $this->user['id'])
        {
            return show(config('code.error'), '服务异常，数据不存在');
        }

        $tarotConfig = config('tarot');

        $cardConfig = $tarotConfig['card_list'][$cardInfo['card_id']];

        $fortuneDayTarotTextModel = new Zl2FortuneDayTarotText();
        $cardData = $fortuneDayTarotTextModel->getInfoById($cardInfo['card_id'], ['title', 'career', 'academic', 'feelings']);
        // 词条内容 待替换
        $resData = [
            'tarot_card' => [
                'card_img' => "/test/tarot/cards/{$cardInfo['card_id']}-1.png",
                'name' => $cardConfig['name'],
                'desc' => $cardConfig['desc'],
                'keyword' => $cardConfig['keyword1'],   // 正位 keyword1  逆位 keyword2
            ],
            'content_list' => [
                [   // 事业
                    'title_img' => '/test/tarot/tarot_career.png',
                    'desc' => $cardData['career'],
                ],
                [   // 学业
                    'title_img' => '/test/tarot/tarot_academic.png',
                    'desc' => $cardData['academic'],
                ],
                [   // 感情
                    'title_img' => '/test/tarot/tarot_feelings.png',
                    'desc' => $cardData['feelings'],
                ]
            ],
            'sub_title' => $cardData['title']
        ];

        return show(config('code.success'), '请求成功', $resData);
    }

    /**
     * 删除 转运卡牌 记录
     * @return Json
     * @throws \Exception
     */
    public function delete()
    {
        $id = input('id/d', 0);
        if (!$id)
        {
            return show(config('code.error'), '请求异常，请稍后再试～');
        }
        $fortuneTarotCardModel = (new Zl2FortuneTarotCard());
        $cardInfo = $fortuneTarotCardModel->getInfoById($id, ['uid', 'delete_time']);

        if (!$cardInfo || $cardInfo['uid'] !== $this->user['id'])
        {
            return show(config('code.error'), '服务异常，数据不存在');
        }

        if ($cardInfo['delete_time'])
        {
            return show(config('code.success'), '删除成功');
        }

        $res = $fortuneTarotCardModel->delData($this->user['id'], $id);

        if ($res)
        {
            return show(config('code.success'), '删除成功');
        } else
        {
            return show(config('code.error'), '服务异常，请稍后再试～');
        }
    }
}